home *** CD-ROM | disk | FTP | other *** search
- Path: news.ov.com!news
- From: glenn@ov.com (Fletcher.Glenn@ov.com)
- Newsgroups: comp.lang.c
- Subject: Re: Can you printf a long
- Date: 4 Apr 1996 23:11:10 GMT
- Organization: OpenVision
- Message-ID: <4k1kue$3cp@spanky.pls.ov.com>
- References: <4ju8o1$dc3@news.netam.net>
- Reply-To: glenn@ov.com
- NNTP-Posting-Host: foghorn.pls.ov.com
-
- In article dc3@news.netam.net, bgc@alpha.netam.net (The Bowling Green Connection) writes:
- >I had some trouble with this code:
- >(I'm using gcc on Digital Unix)
- >
- >int main() {
- > long i;
- >
- > while(1) {
- > i++;
- > printf("%f\n", i);
- > }
- >
- > return 0;
- >}
- >
- >The numbers printed to the screen as 0.00000.
- >(Nevermind that this is an infinite loop--I was piping the output to
- >a file which made me run out of disk space. This is just for testing)
- >
- >But when I changed %f to %d, the numbers printed correctly.
- >I thought that %d had the same limitations as an int..that is,
- >it could only print about 4 bits of information, whereas a long
- >is capable of more (8 or 16 bits), so wouldn't %f (float) be able
- >to better handle those long numbers? And why did %d work
- >correctly, even up to 634,000 (that's when my disk space ran out.. :))
- >
- >Well, then I changed the "long i" declaration to "int i", and changed %f
- >to %d, and I got the SAME results! The numbers went up to 634,000!
- >I didn't think an int could handle this much!
- >
- >Another strange thing is, I did a sizeof(int) and a sizeof(long), and they
- >BOTH returned a 4!!! That can't be right, can it??!
- >
- >_______________________________bgc@bgcky.com___________________________________
- >Edgar E. Easterly, IV http://www.bgcky.com Bowling Green, Kentucky -=O=-
- >"Except the Lord build thy house, ye labour in vain that build it; Except *
- >the Lord keep thy city, the watchman will awake but in vain." Psalms 127:1 |
-
-
- 1. The size of a short, int, and long is implementation dependent.
-
- 2. A long is not a float, and if you use %f you are expecting to print a
- float.
-
- 3. To properly print a long use: "%ld".
-
- 4. On your machine, a long can handle 2,147,483,647 before it overflows,
- and an unsigned long can handle 4,294,967,295.
-
- Fletcher.Glenn@ov.com
-
-